package com.doubao.notepad_blog_backend.jwt;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponseWrapper;
import java.util.*;

public class JwtUtil {
    private static final Logger logger = LoggerFactory.getLogger( JwtUtil.class );
    public static final long EXPIRATION_TIME = 3600_000_000L; // 1000 hour
    public static final String SECRET = "ThisIsASecret";//please change to your own encryption secret.
    public static final String TOKEN_PREFIX = "Bearer ";
    public static final String HEADER_STRING = "Authorization";
    public static final String USER_NAME = "userName";

    public static String generateToken(String userId) {
        Map<String, Object> map = new HashMap<>();
        //you can put any data in the map
        map.put( USER_NAME, userId );
        String jwt = Jwts.builder()
                .setClaims( map )
                .setExpiration( new Date( System.currentTimeMillis() + EXPIRATION_TIME ) )
                .signWith( SignatureAlgorithm.HS512, SECRET )
                .compact();
        return jwt; //jwt前面一般都会加Bearer
    }

    public static HttpServletRequest validateTokenAndAddUserIdToHeader(HttpServletRequest request) {
        String token = request.getHeader( HEADER_STRING );
        if (token != null) {
            try {
                Map<String, Object> body = Jwts.parser()
                        .setSigningKey( SECRET )
                        .parseClaimsJws( token.replace( TOKEN_PREFIX, "" ) )
                        .getBody();
                return new CustomHttpServletRequest(request,body);
            } catch (Exception e) {
                logger.info(e.getMessage());
                // throw new TokenValidationException(e.getMessage());
            }
        }else {
            logger.info( "Missing Token" );
            // throw new TokenValidationException("Missing Token");
        }
        return request;
    }

    public static class CustomHttpServletRequest extends HttpServletRequestWrapper{
        private Map<String,String> claims;

        public CustomHttpServletRequest(HttpServletRequest request, Map<String,?> claims) {
            super( request );
            this.claims = new HashMap<>(  );
            claims.forEach( (k, v) -> this.claims.put(k, String.valueOf( v )) );
        }

        @Override
        public Enumeration<String> getHeaders(String name){
            if(claims != null && claims.containsKey( name )) {
                return Collections.enumeration( Arrays.asList( claims.get( name ) ) );
            }
            return super.getHeaders( name );
        }
    }
}
